﻿using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FraudMitigator
{
    public partial class Reports : Form
    {
        enum RestrictBy { NULL = -1, WEEK = 0, MONTH = 1, YEAR = 2 }
        private string stringToPrint;
        Font printFont;

        public Reports()
        {
            InitializeComponent();
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void printBtn_Click(object sender, EventArgs e)
        {
            WriteDataToFile();

            DialogResult result = printDialog1.ShowDialog();

            // If the result is OK then print the document. 
            if (result == DialogResult.OK)
            {
                using (StreamReader reader = new StreamReader(Directory.GetCurrentDirectory() + "/temp.txt"))
                {
                    stringToPrint = reader.ReadToEnd();
                }
                printFont = new System.Drawing.Font("Times New Roman", 14);
                printDocument1.Print();
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int charactersOnPage = 0;
            int linesPerPage = 0;

            // Sets the value of charactersOnPage to the number of characters 
            // of stringToPrint that will fit within the bounds of the page.
            e.Graphics.MeasureString(stringToPrint, this.Font,
                e.MarginBounds.Size, StringFormat.GenericTypographic,
                out charactersOnPage, out linesPerPage);

            // Draws the string within the bounds of the page
            e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
                e.MarginBounds, StringFormat.GenericTypographic);

            // Remove the portion of the string that has been printed.
            stringToPrint = stringToPrint.Substring(charactersOnPage);

            // Check to see if more pages are to be printed.
            e.HasMorePages = (stringToPrint.Length > 0);
        }

        private void previewBtn_Click(object sender, EventArgs e)
        {
            WriteDataToFile();

            using (StreamReader reader = new StreamReader(Directory.GetCurrentDirectory() + "/temp.txt"))
            {
                stringToPrint = reader.ReadToEnd();
            }

            printFont = new System.Drawing.Font("Times New Roman", 14);
            int taskBarHeight = Screen.PrimaryScreen.Bounds.Bottom - Screen.PrimaryScreen.WorkingArea.Bottom;
            int height = Screen.PrimaryScreen.Bounds.Height;
            int width = Screen.PrimaryScreen.Bounds.Width;

            printPreviewDialog1.Size = new Size((int)(width / 1.9), height - taskBarHeight);
            printPreviewDialog1.DesktopLocation = new Point(0, 0);
            printPreviewDialog1.ShowDialog();
        }

        private void WriteDataToFile()
        {
            using (StreamWriter writer = new StreamWriter(Directory.GetCurrentDirectory() + "/temp.txt", false))
            {
                if (totToCollectCheckBox.Checked)
                {
                    writer.WriteLine("Total amount to collect: " + Queries.getTotalAmtToCollect());
                }

                if (totCollectedCheckBox.Checked)
                {
                    writer.WriteLine("Total amount collected: " + Queries.getTotalAmtCollected());
                }

                if (firstLetterCheckBox.Checked)
                {
                    writer.WriteLine("Number of first letters sent out: " + Queries.getNumFirstLetterSent());
                }

                if (secondLetterCheckBox.Checked)
                {
                    writer.WriteLine("Number of second letters sent out: " + Queries.getNumSecondLetterSent());
                }

                if (thirdLetterCheckBox.Checked)
                {
                    writer.WriteLine("Number of third letters sent out: " + Queries.getNumThirdLetterSent());
                }

                if (openIncidentsCheckBox.Checked)
                {
                    writer.WriteLine("Number of open incidents: " + Queries.getNumOpenIncidents());
                }

                if (closedIncidentsCheckBox.Checked)
                {
                    writer.WriteLine("Number of closed incidents: " + Queries.getNumClosedIncidents());
                }

                if (allIncidentsCheckBox.Checked)
                {
                    writer.WriteLine("Total number of incidents: " + Queries.getNumTotalIncidents());
                }

                if (lettersToSendCheckBox.Checked)
                {
                    writer.WriteLine("Number of letters to be sent: " + Queries.getNumLettersToSend());
                }
            }
        }
    }
}
